package com.xwq.commodityitemsbackgroud.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xwq.api.IProductService;
import com.xwq.entity.TProduct;
import com.xwq.vo.ProductVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    @Reference(check = false)
    IProductService productService;

    @RequestMapping("get/{id}")
    @ResponseBody
    public TProduct getByid(@PathVariable("id")Long id) {
      TProduct product= productService.selectByPrimaryKey(2L);
      return  product;
    }
    @RequestMapping("list")
    public String getListView(Model model) {
        List<TProduct> list = productService.list();
     model.addAttribute("list", list);
        return  "product/list2";
    }

    @RequestMapping("page/{pageIndex}/{pageSize}")
    public String page(Model model,@PathVariable("pageIndex")Integer pageIndex,
                       @PathVariable("pageSize")Integer pageSize) {
        PageInfo<TProduct> pageInfo =productService.pageInfoTProduct(pageIndex, pageSize);
        model.addAttribute("page",pageInfo);
        return  "product/list2";
    }

    @RequestMapping("add")
    public String add (ProductVO vo) {
       Long newId =productService.add(vo);
        return  "redirect:/product/page/1/1";
    }

}
